import React, { useState, useEffect, useMemo } from 'react'
import { ColumnProps, Tag } from '@kedacom/react-next'
import { fetchGetMccMeetingLog } from '@/api/sysLog'
import { MccMeetingLogRequestParam } from '@/api/sysLog/interface'
import { MccMeetingLogData } from '../interface'
import Alert from '@/components/Alert'
import dayjs from '@/plugins/dayjs'
import { transFetchMccMeetingLogData } from '../translate'
import useLock from '@/hooks/useLock'
import { t } from 'i18next'

const pageSize = 20

export default function useService() {
	const defaultParams: MccMeetingLogRequestParam = {
		count: pageSize,
		start: 0,
		operator: '',
		meeting_name: '',
		start_date: dayjs().subtract(7, 'day').format('YYYY-MM-DD'),
		end_date: dayjs().format('YYYY-MM-DD'),
		order: 1,
	}
	const [data, setData] = useState<MccMeetingLogData[]>([])
	const [total, setTotal] = useState(0)
	const [isSearch, setIsSearch] = useState(false)
	const [params, setParams] = useState<MccMeetingLogRequestParam>(defaultParams)
	const pager = useMemo(() => {
		return { page: params.start / pageSize + 1, size: pageSize, total }
	}, [params.start, pageSize, total])

	useEffect(() => {
		queryData(params, true)
	}, [])

	const columns: ColumnProps<MccMeetingLogData>[] = [
		{ title: t('会议名称'), dataKey: 'name' },
		{ title: t('会议发起人'), dataKey: 'creator', width: 158 },
		{ title: t('开始时间'), dataKey: 'start_time', width: 164 },
		{ title: t('结束时间'), dataKey: 'end_time', width: 164 },
		{ title: t('完整性校验'), dataKey: 'integrity_check', width: 158 },
		{
			title: t('操作'),
			dataKey: 'action',
			width: 88,
			render: (rowData, value) => {
				return <Tag onClick={() => toMccDetail(rowData)}>{t('详情')}</Tag>
			},
		},
	]

	const toMccDetail = (data: MccMeetingLogData) => {
		let url = `./meetingDetailLog.html?meetingMoid=${data.meeting_moid}&meetingName=${data.name}&meetingCreator=${data.creator}&meetingStartTime=${data.start_time}&meetingEndTime=${data.end_time}`
		window.location.href = url
	}

	const changeParam = (data: ReducerMaker<MccMeetingLogRequestParam>) => {
		const { type, payLoad } = data
		switch (type) {
			case 'start':
				setParams({ ...params, start: payLoad })
				break
			case 'operator':
				setParams({ ...params, operator: payLoad })
				break
			case 'meeting_name':
				setParams({ ...params, meeting_name: payLoad })
				break
			case 'start_date':
				setParams({ ...params, start_date: payLoad })
				break
			case 'end_date':
				setParams({ ...params, end_date: payLoad })
				break
			case 'order':
				setParams({ ...params, order: payLoad })
				break
		}
	}

	const onSearch = () => {
		setIsSearch(true)
		let newParams = { ...params, start: 0 }
		queryData(newParams)
		setParams(newParams)
	}

	const queryData = useLock(async (param: MccMeetingLogRequestParam, isInit?: boolean) => {
		await fetchGetMccMeetingLog(param).then((res) => {
			if (res) {
				let data = transFetchMccMeetingLogData(res.data)
				setData(data)
				setTotal(res.total)
			} else {
				isInit ? Alert(t('获取会场管理日志失败！')) : setIsSearch(false)
			}
		})
	})

	const resetSettingParam = () => {
		setIsSearch(false)
		setParams(defaultParams)
		queryData(defaultParams)
	}

	const onPageChange = (pageIndex: number, pageSize: number) => {
		let start = (pageIndex - 1) * pageSize
		queryData({ ...params, start })
		changeParam({ type: 'start', payLoad: start })
	}

	return {
		/** table columns */
		columns,
		/** 列表数据 */
		data,
		/** 搜索请求参数 */
		params,
		/** 是否处于搜索状态 */
		isSearch,
		/** 分页器参数 */
		pager,
		/** 修改搜索请求参数 */
		changeParam,
		/** 请求方法 */
		onSearch,
		/** 重置搜索请求参数 */
		resetSettingParam,
		/** 分页回调 */
		onPageChange,
	}
}
